package com.adv.ems.filters.report.after;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.adv.ems.db.entity.EnergyDataCumulateReportMonthDO;
import com.adv.ems.db.entity.EnergyDataCumulateReportYearDO;
import com.adv.ems.filters.EnergyDataBaseFilter;
import com.adv.ems.filters.dto.FilterParam;
import com.adv.ems.filters.report.dto.ReportHourPoolDTO;
import com.adv.ems.manager.EnergyDataCumulateReportMonthManager;
import com.adv.ems.manager.EnergyDataCumulateReportYearManager;
import com.adv.ems.model.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Component
public class YearReportAfterFilter extends EnergyDataBaseFilter<ReportHourPoolDTO, Boolean> {
    @Resource
    EnergyDataCumulateReportMonthManager energyDataCumulateReportMonthManager;
    @Resource
    EnergyDataCumulateReportYearManager energyDataCumulateReportYearManager;

    @Override
    protected String getFilterName() {
        return "报表年数据统计";
    }

    @Override
    protected double getOrder() {
        return 4;
    }

    @Override
    protected boolean isBefore() {
        return false;
    }

    @Override
    protected String getType() {
        return ReportHourPoolDTO.class.getName();
    }

    @Override
    protected ResultVO<Boolean> handle(FilterParam<ReportHourPoolDTO> param) {
        Map<Long, Set<String>> colMonthMap = new HashMap<>();
        param.getList().forEach(h -> {
            Long pointId = h.getPointId();
            String day = DateUtil.formatDate(h.getTime()).substring(0, 4) + "-01-01";
            Set<String> orDefault = colMonthMap.getOrDefault(pointId, new HashSet<>());
            orDefault.add(day);
            colMonthMap.put(pointId, orDefault);
        });
        for (Map.Entry<Long, Set<String>> entry : colMonthMap.entrySet()) {
            Long pointId = entry.getKey();
            entry.getValue().forEach(y -> {
                rebuildMonthReport(pointId, y);
            });
        }
        return ResultVO.success(true);
    }

    private void rebuildMonthReport(Long pointId, String yearStr) {
        log.info("统计采集点ID【{}】统计时间：【{}】开始", pointId, yearStr);
        DateTime dateTime = DateUtil.parseDate(yearStr);
        Date start = dateTime.toJdkDate();
        Date end = new Date(DateUtil.offset(start, DateField.YEAR, 1).getTime() - 1);
        List<EnergyDataCumulateReportMonthDO> dayDataList = energyDataCumulateReportMonthManager.getAllDataByPonitTime(pointId, start, end);
        Double increaseRealValue = 0d;
        Double increaseStandValue = 0d;
        Double increaseCashValue = 0d;
        Long firstId = null;
        Double firstValue = null;
        Long lastId = null;
        Double lastValue = null;
        for (EnergyDataCumulateReportMonthDO data : dayDataList) {
            if (firstId == null) {
                firstId = data.getFirstId();
                firstValue = data.getFirstRealValue();
            }
            lastId = data.getLastId();
            lastValue = data.getLastRealValue();
            increaseRealValue = data.getIncreaseRealValue() + increaseRealValue;
            increaseStandValue = data.getIncreaseStandValue() + increaseStandValue;
            increaseCashValue = data.getIncreaseCashValue() + increaseCashValue;
        }
        EnergyDataCumulateReportYearDO reportYear = new EnergyDataCumulateReportYearDO();
        reportYear.setCollectPointId(pointId);
        reportYear.setFirstId(firstId);
        reportYear.setFirstRealValue(firstValue);
        reportYear.setLastId(lastId);
        reportYear.setLastRealValue(lastValue);
        reportYear.setIncreaseRealValue(increaseRealValue);
        reportYear.setIncreaseStandValue(increaseStandValue);
        reportYear.setIncreaseCashValue(increaseCashValue);
        reportYear.setColDate(start);
        reportYear.setCreateTime(new Date());
        energyDataCumulateReportYearManager.deleteByPointColDate(pointId, start);
        energyDataCumulateReportYearManager.insertData(reportYear);
        log.info("统计采集点ID【{}】统计时间：【{}】结束", pointId, yearStr);
    }
}
